package com.acwing.partition11;

import java.io.*;

/**
 * @author `RKC`
 * @date 2021/12/20 21:50
 */
public class AC1068环形石子合并 {

    private static final int INF = 0x3f3f3f3f;

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
        String[] s = reader.readLine().split(" ");
        int n = Integer.parseInt(s[0]);
        int[] stones = new int[n];
        s = reader.readLine().split(" ");
        for (int i = 0; i < s.length; i++) {
            stones[i] = Integer.parseInt(s[i]);
        }
        int[] answer = dynamicProgramming(stones);
        writer.write(answer[0] + "\n");
        writer.write(answer[1] + "\n");
        writer.flush();
    }

    private static int[] dynamicProgramming(int[] nums) {
        int n = nums.length;
        int[] stones = new int[n << 1], prefix = new int[n * 2 + 1];
        //扩容，预处理出前缀和
        for (int i = 0; i < n; i++) stones[i] = stones[i + n] = nums[i];
        for (int i = 1; i <= n * 2; i++) prefix[i] += prefix[i - 1] + stones[i - 1];
        //分别表示区间i和j之间的最小和最大
        int[][] dp1 = new int[n * 2][n * 2], dp2 = new int[n * 2][n * 2];
        for (int i = 0; i < n * 2; i++) {
            for (int j = 0; j < n * 2; j++) {
                if (i == j) continue;
                dp1[i][j] = INF;
                dp2[i][j] = -INF;
            }
        }
        for (int length = 2; length <= stones.length; length++) {
            for (int i = 0; i + length - 1 < n << 1; i++) {
                int j = i + length - 1;
                for (int k = i; k < j; k++) {
                    dp1[i][j] = Math.min(dp1[i][j], dp1[i][k] + dp1[k + 1][j] + prefix[j + 1] - prefix[i]);
                    dp2[i][j] = Math.max(dp2[i][j], dp2[i][k] + dp2[k + 1][j] + prefix[j + 1] - prefix[i]);
                }
            }
        }
        //枚举环型中的答案
        int[] answer = { INF, -INF };
        for (int i = 0; i <= n; i++) {
            answer[0] = Math.min(answer[0], dp1[i][i + n - 1]);
            answer[1] = Math.max(answer[1], dp2[i][i + n - 1]);
        }
        return answer;
    }
}
